/*
 * File:   HotelItem.cpp
 * Author: AbDuLlaH
 *
 * Created on February 5, 2015, 2:14 PM
 */

#include "HotelItem.h"

HotelItem::HotelItem() {
}

HotelItem::HotelItem(string ID,string name,string add,
                     string country,string district,string cat)
{
    this->hotelID = ID;
    this->name = name;
    address = add;
    country = country;
    district = district;
    category = cat;
}

void HotelItem::setHotelID(string ID)
{
    this->hotelID = ID;
}

void HotelItem::setName(string name)
{
    this->name = name;
}

void HotelItem::setAddress(string add)
{
    this->address = add;
}

void HotelItem::setCountry(string country)
{
    this->country = country;
}

void HotelItem::setDistrict(string district)
{
    this->district = district;
}

void HotelItem::setCategory(string cat)
{
    this->category = cat;
}

string HotelItem::getHotelID()
{
    return this->hotelID;
}

string HotelItem::getName()
{
    return this->name;
}

string HotelItem::getAddress()
{
    return this->address;
}

string HotelItem::getCountry()
{
    return this->country;
}

string HotelItem::getDistrict()
{
    return this->district;
}

string HotelItem::getCategory()
{
    return this->category;
}

void HotelItem::addHotelItem()
{
    
    HotelItem createhotel;
    createhotel.readFile();
    cout<<"\n\n\n";
    cout<<"Hotel Item To Add!!"<<endl;
    
    
    cin>>createhotel;
    
    if(confirmation()){
        
        tempVector.push_back(createhotel);
    }
    
}

void HotelItem::deleteHotelItem()
{
    int count=0;
    p=tempVector.begin();
    
    while(p != tempVector.end()){
        
        cout<<"["<<count<<"] "<<(*p);
        count++;
        p++;
    }
    
    cout<<"Which one to delete?:";
    cin>>count;
    
    if(confirmation()) {
        p=tempVector.begin();
        for(int i=0;i<count;i++){
            
            p++;
        }
        
        tempVector.erase(p);
    }
}

void HotelItem::modifyHotelItem()
{
    int count=1;
    int choice;
    string temp;
    
    p=tempVector.begin();
    while(p != tempVector.end()){
        
        cout<<"["<<count<<"] "<<(*p);
        count++;
        p++;
    }
    
    cout<<"Which one to modify?:";
    cin>>count;
    
    p=tempVector.begin();
    for(int i=0;i<count-1;i++){
        p++;
    }
    
    cout<<
    "What do you want to modify?"<<endl<<
    "1)\tName"<<endl<<
    "2)\tAddress"<<endl<<
    "3)\tPlace Of Origin"<<endl<<
    "4)\tDistrict Of Origin"<<endl<<
    "5)\tCategory"<<endl;
    
    cout<<"Choice:";
    cin>>choice;
    
    cin.ignore(100,'\n');
    
    switch(choice){
            
        case 1:
        {
            cout<<"Please Enter New Name:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getName()<<" -->  "<<temp<<endl;
                (*p).setName(temp);
            }
            break;
        }
        case 2:
        {
            
            cout<<"Please Enter New Address:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getAddress()<<" -->  "<<temp<<endl;
                (*p).setAddress(temp);
            }
            break;
        }
        case 3:
        {
            cout<<"Please Enter New Place Of Origin:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getCountry()<<" -->  "<<temp<<endl;
                (*p).setCountry(temp);
            }
            break;
        }
        case 4:
        {
            cout<<"Please Enter New District:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getDistrict()<<" -->  "<<temp<<endl;
                (*p).setDistrict(temp);
            }
            break;
        }
        case 5:
        {
            cout<<"Please Enter New Category:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getCategory()<<" -->  "<<temp<<endl;
                (*p).setCategory(temp);
            }
            break;
        }
        default:
        {
            cout<<"wrong input please re enter:";
            cin>>choice;
        }
    }
    
}

void HotelItem::display(){
    
    p=tempVector.begin();
    
    while(p!= tempVector.end()){
        
        cout<<(*p);
        p++;
    }
}

void HotelItem::readFile(){
    
    stringstream ss;
    fstream infile;
    string open="hitems.txt";
    
    string id;
    string nam;
    string add;
    string ctry;
    string dist;
    string cat;
    
    HotelItem aHotelItem;
    tempVector.clear();
    
    infile.open(open.c_str());
    
    if(!infile){
        
        cout<<"Sorry file corrupted please contact Database Adminstrator"<<endl;
    }
    else{
        
        getline(infile, id, '|');
        getline(infile, nam, '|');
        getline(infile, add, '|');
        getline(infile, ctry, '|');
        getline(infile, dist, '|');
        getline(infile, cat, '|');
        
        aHotelItem.setHotelID(id);
        aHotelItem.setName(nam);
        aHotelItem.setAddress(add);
        aHotelItem.setCountry(ctry);
        aHotelItem.setDistrict(dist);
        aHotelItem.setCategory(cat);
        
        tempVector.push_back(aHotelItem);
        
        while(!infile.eof()){
            
            
            getline(infile, id, '|');
            id.erase(0,1);
            getline(infile, nam, '|');
            getline(infile, add, '|');
            getline(infile, ctry, '|');
            getline(infile, dist, '|');
            getline(infile, cat, '|');
            
            aHotelItem.setHotelID(id);
            aHotelItem.setName(nam);
            aHotelItem.setAddress(add);
            aHotelItem.setCountry(ctry);
            aHotelItem.setDistrict(dist);
            aHotelItem.setCategory(cat);
            
            tempVector.push_back(aHotelItem);
            
        }
        tempVector.pop_back();
        
    }
    infile.close();
    
}

void HotelItem::writeFile(){
    
    ofstream outFile;
    string opene="hitems.txt";
    p=tempVector.begin();
    outFile.open(opene.c_str());
    
    while(p!=tempVector.end()){
        
        outFile<<(*p).getHotelID()<<"|"<<(*p).getName()<<"|"<<(*p).getAddress()<<"|"<<(*p).getCountry()<<"|"<<(*p).getDistrict()<<"|"<<(*p).getCategory()<<"|"<<endl;
        
        p++;
        
    }
    outFile.close();
    
    
}

bool HotelItem::confirmation(){
    
    char confirm;
    cout<<"Are you sure?(y/n):";
    cin>>confirm;
    
    while(!(confirm =='y' || confirm == 'n'));
    switch(confirm){
            
        case 'y':
        {
            return true;
        }
        case 'n':
        {
            return false;
        }
        default:
        {
            cout<<"Wrong input. Please re-enter:";
            cin>>confirm;
        }
    }
    return false;
}

ostream& operator<<(ostream& out, HotelItem& anitem){
    
    out<<anitem.getHotelID()<<","<<anitem.getName()<<","<<anitem.getAddress()<<","<<anitem.getCountry()<<","<<anitem.getDistrict()<<","<<anitem.getCountry()<<endl;
    
    return out;
}

istream& operator>>(istream& in, HotelItem& anitem){
    
    string eid;
    string nme;
    string addr;
    string contry;
    string distr;
    string catr;
    
    
    
    cout<<"Enter HotelID:";
    cin>>eid;
    anitem.setHotelID(eid);
    
    cin.ignore(100,'\n');
    
    cout<<"Enter Hotel Name:";
    getline(cin, nme);
    anitem.setName(nme);
    
    cout<<"Enter Hotel Address:";
    getline(cin,addr);
    anitem.setAddress(addr);
    
    cout<<"Enter Country of Origin:";
    getline(cin,contry);
    anitem.setCountry(contry);
    
    cout<<"Enter District of Origin:";
    getline(cin,distr);
    anitem.setDistrict(distr);
    
    cout<<"Enter Category:";
    getline(cin,catr);
    anitem.setCategory(catr);
    
    return in;
}